package com.ec.activemq.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

/**
 * activeMq消费类
 *
 * @Author Eric
 * @Date 2022/7/6 11:35
 */
@Component
public class ActiveConsumerListener {

    private static final Logger log = LoggerFactory.getLogger(ActiveConsumerListener.class);

    /**
     * 使用JmsListener配置消费者监听的队列，其中name是接收到的消息
     *
     * @param message
     * @return
     */
    @JmsListener(destination = "${spring.activemq.queue-name:ec-queue}", containerFactory = "jmsListenerContainerQueue")
    @SendTo("ec-queue")
    public void receiveQueue(final TextMessage message) throws JMSException {
        String messageText = message.getText();
        log.info("queue消费完成：activemq收到的消息：{}", messageText);
    }

    @SendTo("ec-topic")
    @JmsListener(destination = "${spring.activemq.topic-name:ec-topic}", containerFactory = "jmsListenerContainerTopic")
    public void receiveTopic(final TextMessage message) throws JMSException {
        String messageText = message.getText();
        log.info("topic消费完成：activemq收到的消息：{}", messageText);
    }

}